package com.gmq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gmq.common.R;
import com.gmq.entity.User;
import com.gmq.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    public R<String> login(@RequestBody User user, HttpServletRequest request) {
        //查询是否存在手机号,不存在的话就往数据库中写入
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getPhone, user.getPhone());
        queryWrapper.like(User::getStatus, 1);
        int count = userService.count(queryWrapper);
        if (count == 0) {
            user.setStatus(1);
            userService.save(user);
        }
        User one = userService.getOne(queryWrapper);
        request.getSession().setAttribute("userId", one.getId());
        return R.success("登录成功");
    }

}
